package com.itheima.algorithm;

public class InsertionSort {
    public static void main(String[] args) {
        int[] a = {9,3,1,4,6,8,7,5,2};
        sort(a);
        print(a);
    }

    //比较
    static void sort(int[] a){
        for(int i = 1; i < a.length; i++){
            for(int j = i; j > 0; j--){
                if(a[j] < a[j - 1]){
                    swap(a, j ,j - 1);
                }
            }
        }

    }

    //交换
    static void swap(int[] a, int i, int j){
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    //打印
    static void print(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
